1496A - Split it - CodeForces Solution


brute force constructive algorithms greedy strings *900

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase


def main():
    t = int(input())
    for _ in range(t):
        n, k = read_ints()
        s = input()
        if n < 2 * k + 1:
            print('NO')
        else:
            for i in range(k):
                if s[i] != s[n - i - 1]:
                    print('NO')
                    break
            else:
                    print('YES')



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



def read_ints():
    return [int(c) for c in input().split()]


def print_int_lines(lst):
    print('\n'.join(map(str, lst)))


if __name__ == "__main__":
    main()

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string.h>
#include<bitset>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#include<cstdlib>
#include<sstream>
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define test_case int t;cin>>t;while(t--)
typedef long long int ll;
typedef long double ld;
using namespace std;
void Abdo_Saad() { std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); }
ll gcd(ll a, ll b) { return(b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void set_bit(int& n, int idx) { n |= 1 << idx; }
void reset_bit(int& n, int idx) { n &= ~(1 << idx); }
void flib_bit(int& n, int idx) { n ^= 1 << idx; }
bool pal(string s)
{
	int n = s.size();
	for (int i = 0;i< n/2;i++)
	{
		if (s[i] != s[n - i - 1])return false;
	}
	return true;
}
void solve()
{
	int n, k;
	cin >> n >> k;
	string s;
	cin >> s;
	if (!k)
	{
		cout << "YES\n";
		return;
	}
	if (2 * k == n)
	{
		cout << "NO\n";
		return;
	}
	string t1, t2;
	for (int i = 0;i < k;i++)
	{
		t1.push_back(s[i]);
		t2.push_back(s[n - i-1]);
	}
	reverse(all(t2));
	if (pal(t1 + t2))cout << "YES\n";
	else cout << "NO\n";
	
		
	
}
int main()
{
	Abdo_Saad();
	test_case
	solve();
}


Comments

Submit
0 Comments
More Questions

466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers